# model

# merge the former classes "AlleKeysLoeschenFrage" and "AlleKeysLoeschenDefinitiv"
# therefore to a current user with his credentials, all bucket-keys will be loaded
# then the keys could be deleted
# before the delete-process will be performed a final question will be asked... 

from google.appengine.ext import webapp

from controller.BuildStorageInfo import *

from Utilities import *

class DeleteAllBucketKeys(webapp.RequestHandler):
    def get(self):
        utilities = Utilities()
        
        # get some necessary information
        userText = utilities.getUserText()
        username = utilities.getUsername()
        amazon = utilities.getAmazonText()
        google = utilities.getGoogleText()
        openStack = utilities.getOpenStackText()
        
        if username:
            urlText = utilities.getUrlTextLogout()
            url = utilities.getLogout(self)
            
            # initialize the storage-variables
            amazonStorageConnection = None
            googleStorageConnection = None
            openStackStorageConnection = None
            
            # determine the given credentials
            credentials = utilities.getCredentials(username)
            results = credentials.fetch(100)
            
            # determine the logged in storage-services
            for result in results:
                storageService = result.credentialType
                
                if storageService == amazon:
                    amazonStorageService = storageService
                    amazonStorageConnection, regionname = utilities.getStorageServiceConnection(username, amazonStorageService)
                
                if storageService == google:
                    googleStorageService = storageService
                    googleStorageConnection, regionname = utilities.getStorageServiceConnection(username, googleStorageService)
                
                if storageService == openStack:
                    openStackStorageService = storageService
                    openStackStorageConnection, regionname = utilities.getStorageServiceConnection(username, openStackStorageService)

            # determine the bucketname
            bucketname = utilities.getBucketname(username)
                    
            # initialize the key-lists for the available storages
            keyListAmazon = []
            keyListGoogle = []
            keyListOpenStack = []
            
            # get the keys from the specific storage-services
            if amazonStorageConnection != None:
#                keyListAmazon = utilities.getBucketKeyList(self, amazonStorageConnection, bucketname, keyListAmazon)
                keyListAmazon = utilities.getBucketKeyList(amazonStorageConnection, bucketname)
            
            if googleStorageConnection != None:
#                keyListGoogle = utilities.getBucketKeyList(self, googleStorageConnection, bucketname, keyListGoogle)
                keyListGoogle = utilities.getBucketKeyList(googleStorageConnection, bucketname)
            
            if openStackStorageConnection != None:
#                keyListOpenStack = utilities.getBucketKeyList(self, openStackStorageConnection, bucketname, keyListOpenStack)
                keyListOpenStack = utilities.getBucketKeyList(openStackStorageConnection, bucketname)

            # delete the keys inside the keyList for each available storage-service
            isAmazonDeleted = utilities.deleteKeys(amazon, keyListAmazon)
            isGoogleDeleted = utilities.deleteKeys(google, keyListGoogle)
            isOpenStackDeleted = utilities.deleteKeys(openStack, keyListOpenStack)
            isLocalDatabseInfoDeleted = utilities.deleteLocalKeys()

            if isAmazonDeleted == None or isGoogleDeleted == None or isOpenStackDeleted == None or isLocalDatabseInfoDeleted == None:
#                hintText = _('An error occurred - the keys could not be deleted')
                hintText = 'An error occurred - the keys could not be deleted'
                hintColor = 'red'
                
                self.callBuilder(userText, username, url, urlText, hintText, hintColor, keyListAmazon)
                self.redirect('/selectdata')
            elif isAmazonDeleted == 'true' and isGoogleDeleted == 'true' and isOpenStackDeleted == 'true':
#                hintText = _('The keys were deleted successfully')
                hintText = 'The keys were deleted successfully'
                hintColor = 'green'
                
                self.callBuilder(userText, username, url, urlText, hintText, hintColor, keyListAmazon)
                self.redirect('/selectdata')         
        else:
            self.redirect('/')
            
    # responsible for the call of the get-method of the BuildStorageInfo-Class
    def callBuilder(self, userText, username, url, urlText, hintText, hintColor, keyList):
        templateValues = {
                          'userText': userText,
                          'user': username,
                          'urlText': urlText,
                          'url': url,
                          'urlText': urlText,
                          'hintText': hintText,
                          'hintColor': hintColor
                          }
        
        builder = BuildStorageInfo()
        builder.get(self, templateValues, keyList)    
        
